/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdSeguridadAdministracionLogueo.java
 *
 * Created on 13/05/2010, 09:58:01 AM
 */

package gestionseguridadadministrativacliente;

import administracion.delegate.GestionSeguridadDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.propiedades.Propiedades;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;



/**
 *
 * @author Renato
 */
public class jdSeguridadCambioPassword extends javax.swing.JDialog {
    GestionSeguridadAdministrativaclienteApp appPrincipal;
    boolean cargarSistemaPrincipal;
    String usuario;
    Boolean estado=false;
    /** Creates new form jdSeguridadAdministracionLogueo */
    public jdSeguridadCambioPassword(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    public jdSeguridadCambioPassword(GestionSeguridadAdministrativaclienteApp app, boolean modal,boolean cargarSistema) {
    super(app.getMainFrame(), modal);
    this.appPrincipal = app;
    cargarSistemaPrincipal=cargarSistema;
    initComponents();
  }
    public jdSeguridadCambioPassword(GestionSeguridadAdministrativaclienteApp app, boolean modal,boolean cargarSistema, String user) {
    super(app.getMainFrame(), modal);
    this.appPrincipal = app;
    cargarSistemaPrincipal=cargarSistema;
    this.usuario=user;
    this.estado=true;
    initComponents();
    
  }
     public void correrAlerta(){
         try {
            int tamaño=Listas.obtenerAlertasTamaño();
            String alerta= "Usted tiene "+ tamaño+ " nuevas alertas";
            Toaster toasterManager = new Toaster();
            toasterManager.showToaster( new ImageIcon(appPrincipal.getClass().getResource("/gestionseguridadadministrativacliente/resources/alerta.png")),alerta );         
        } catch (Exception ex) {
            ex.printStackTrace();
        }
     }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jbAceptar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jpfAnterior = new javax.swing.JPasswordField();
        jpfClaveNueva = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jpfClaveNuevaCopia = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdSeguridadCambioPassword.class);
        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jbAceptar.setFont(resourceMap.getFont("jbAceptar.font")); // NOI18N
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText(resourceMap.getString("jbAceptar.text")); // NOI18N
        jbAceptar.setEnabled(false);
        jbAceptar.setName("jbAceptar"); // NOI18N
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText(resourceMap.getString("jbCancelar.text")); // NOI18N
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jpfAnterior.setText(resourceMap.getString("jpfAnterior.text")); // NOI18N
        jpfAnterior.setName("jpfAnterior"); // NOI18N
        jpfAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jpfAnteriorActionPerformed(evt);
            }
        });

        jpfClaveNueva.setEnabled(false);
        jpfClaveNueva.setName("jpfClaveNueva"); // NOI18N
        jpfClaveNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jpfClaveNuevaActionPerformed(evt);
            }
        });

        jLabel5.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jpfClaveNuevaCopia.setEnabled(false);
        jpfClaveNuevaCopia.setName("jpfClaveNuevaCopia"); // NOI18N
        jpfClaveNuevaCopia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jpfClaveNuevaCopiaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(52, 52, 52)
                                .addComponent(jbAceptar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbCancelar))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(35, 35, 35)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jpfClaveNuevaCopia)
                                    .addComponent(jpfClaveNueva)
                                    .addComponent(jpfAnterior, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbAceptar, jbCancelar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jpfAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jpfClaveNueva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jpfClaveNuevaCopia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbAceptar)
                            .addComponent(jbCancelar))))
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed

      try {
          String pass_nueva = "";
          String pass_nueva_copia = "";

          pass_nueva=String.valueOf(jpfClaveNueva.getPassword());
          pass_nueva_copia=String.valueOf(jpfClaveNuevaCopia.getPassword());
          if(pass_nueva.trim().equalsIgnoreCase("") || pass_nueva_copia.trim().equalsIgnoreCase("")){
              Utiles_MensajesDialogo.mensajeError("Los campos no deben ser vacios");
              return;
          }
          if(!pass_nueva.trim().equalsIgnoreCase(pass_nueva_copia.trim())){
              Utiles_MensajesDialogo.mensajeError("Las contraseñas no coinciden");
              return;
          }
          GestionSeguridadDelegate gestion = GestionSeguridadDelegate.getInstance();
          boolean respuesta = gestion.actualizarClave(Propiedades.getUsuario(), pass_nueva);
          if (respuesta) {
                Utiles_MensajesDialogo.mensajeExito();
                limpiar();
          }
          else
              Utiles_MensajesDialogo.mensajeError("No se pudo realizar la operacion");
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    }//GEN-LAST:event_jbAceptarActionPerformed


    private void limpiar(){

        this.jpfAnterior.setText("");
        this.jpfClaveNueva.setText("");
        this.jpfClaveNuevaCopia.setText("");


        this.jbAceptar.setEnabled(false);
        this.jpfClaveNueva.setEnabled(false);
        this.jpfClaveNuevaCopia.setEnabled(false);
        this.jpfAnterior.requestFocus();

    }

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
       this.dispose();
    }//GEN-LAST:event_jbCancelarActionPerformed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed
        
        
    }//GEN-LAST:event_jbAceptarKeyPressed

    private void jpfAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jpfAnteriorActionPerformed
         try {
          String user = "";
          String pass = "";

          pass=String.valueOf(jpfAnterior.getPassword());
          if(pass.trim().equalsIgnoreCase("")){
              Utiles_MensajesDialogo.mensajeError("El campo no debe ser vacio");
              return;
          }
          GestionSeguridadDelegate gestion = GestionSeguridadDelegate.getInstance();
          boolean entrar = gestion.verificarClave(Propiedades.getUsuario(), pass);
          if (entrar) {
                this.jpfClaveNueva.setEnabled(true);
                this.jpfClaveNuevaCopia.setEnabled(true);
                this.jbAceptar.setEnabled(entrar);
                this.jpfClaveNueva.requestFocus();
          }
          else{
              Utiles_MensajesDialogo.mensajeError("Clave incorrecta");
          }
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    }//GEN-LAST:event_jpfAnteriorActionPerformed

    private void jpfClaveNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jpfClaveNuevaActionPerformed
       this.jpfClaveNuevaCopia.requestFocus();
    }//GEN-LAST:event_jpfClaveNuevaActionPerformed

    private void jpfClaveNuevaCopiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jpfClaveNuevaCopiaActionPerformed
        this.jbAceptar.requestFocus();
    }//GEN-LAST:event_jpfClaveNuevaCopiaActionPerformed

    /**
    * @param args the command line arguments
    */
   
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JPasswordField jpfAnterior;
    private javax.swing.JPasswordField jpfClaveNueva;
    private javax.swing.JPasswordField jpfClaveNuevaCopia;
    // End of variables declaration//GEN-END:variables

    class HiloIniSist extends Thread{
        HiloIniSist(  ){


        }
        public void run(){
            try {
                Listas.cargarListas();
                correrAlerta();
                appPrincipal.show(new GestionSeguridadAdministrativaclienteView(appPrincipal));
                //correrAlerta();
            } catch (Exception ex) {
                Logger.getLogger(jdSeguridadCambioPassword.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        private String nomb;
        private String id;
        private String tipoUser;
    }
}
